import axios from 'axios'
import {
  getToken
} from '@/utils/index'

const url = {
  baseUrl: {
    development: 'http://www.pack-mate.com/api/'
    // development: 'https://www.soumikeji.cn/api/'
  }
}

/**
 * axios封装
 */
class Request {
  constructor() {
    this.baseUrl = url.baseUrl['development']
  }

  interceptors(instance) {
    instance.interceptors.request.use(config => {
      return config
    }, err => {
      return Promise.reject(err)
    })

    instance.interceptors.response.use(res => {
      return res
    }, err => {
      return Promise.reject(err)
    })
  }

  request(config) {

    const instance = axios.create()
    const apiAuth = getToken()
    let headers = apiAuth ? {
      apiAuth
    } : {}
    if (config.headers) {
      headers = config.headers;
    }

    config = Object.assign({
      baseURL: this.baseUrl,
      headers
    }, config)

    this.interceptors(instance)
    return instance(config)
  }
}

const http = new Request()

export default http
